/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import java.util.ArrayList;

/**
 *
 * @author Steffen
 */
public class Invoice {
    private int invoiceid;
    private String paymentDate = "";
    private int amount;
    private int status;
    private float discount;
    private float totalPrice;

    private Customer customer = null;

    private ArrayList<Order> orders = new ArrayList<Order>();

    public Invoice (int invoiceid, Customer customer, String paymentDate, int amount, int status, float discount, float totalPrice)
    {
        this.customer = customer;
        this.invoiceid = invoiceid;
        this.paymentDate = paymentDate;
        this.amount = amount;
        this.status = status;
        this.discount = discount;
        this.totalPrice = totalPrice;
    }

    public Invoice (Customer customer, String paymentDate, int amount, int status, float discount, float totalPrice)
    {
        this.customer = customer;
        this.paymentDate = paymentDate;
        this.amount = amount;
        this.status = status;
        this.discount = discount;
        this.totalPrice = totalPrice;
    }

    public Invoice (int invoiceid, String paymentDate, int amount, int status, float discount, float totalPrice)
    {
        this.invoiceid = invoiceid;
        this.paymentDate = paymentDate;
        this.amount = amount;
        this.status = status;
        this.discount = discount;
        this.totalPrice = totalPrice;
    }

    public Invoice (String paymentDate, int amount, int status, float discount, float totalPrice)
    {
        this.paymentDate = paymentDate;
        this.amount = amount;
        this.status = status;
        this.discount = discount;
        this.totalPrice = totalPrice;
    }

    public Invoice ()
    {

    }

    public void setOrders(ArrayList<Order> orders) {
        this.orders = orders;
        calculateTotalPrice();
    }

    public Order getOrder(int index) {
        return orders.get(index);
    }

    public ArrayList<Order> getOrders() {
        return orders;
    }

    public boolean addOrder(Order order) {
        boolean result = orders.add(order);
        calculateTotalPrice();
        return result;
    }

    public boolean removeOrder(Order order) {
        boolean result = orders.remove(order);
        calculateTotalPrice();
        return result;
    }

    public boolean removeOrder(int orderId) {
        boolean removed = false;
        for(Order o : orders) {
            if(o.getId() == orderId) {
                removed = removeOrder(o);
                break;
            }
        }
        calculateTotalPrice();
        return removed;
    }

    public Customer getCustomer() {
        return this.customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the invoiceid
     */
    public int getInvoiceid() {
        return invoiceid;
    }

    /**
     * @param invoiceid the invoiceid to set
     */
    public void setInvoiceid(int invoiceid) {
        this.invoiceid = invoiceid;
    }

    /**
     * @return the paymentDate
     */
    public String getPaymentDate() {
        return paymentDate;
    }

    /**
     * @param paymentDate the paymentDate to set
     */
    public void setPaymentDate(String paymentDate) {
        this.paymentDate = paymentDate;
    }

    /**
     * @return the amount
     */
    public int getAmount() {
        return amount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(int amount) {
        this.amount = amount;
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(int status) {
        this.status = status;
    }

    /**
     * @return the discount
     */
    public float getDiscount() {
        return discount;
    }

    /**
     * @param discount the discount to set
     */
    public void setDiscount(float discount) {
        this.discount = discount;
    }

    /**
     * @return the totalPrice
     */
    public float getTotalPrice() {
        return totalPrice;
    }

    /**
     * @param totalPrice the totalPrice to set
     */
    public void setTotalPrice(float totalPrice) {
        this.totalPrice = totalPrice;
    }

    private void calculateTotalPrice() {
        float t = 0;
        for(Order o : orders) {
            t += o.getTotalPrice();
        }

        this.totalPrice = t;
        this.updateAmount();
    }

    private void updateAmount() {
        int a = 0;
        for(Order o : orders) {
            a += o.getAmount();
        }

        this.amount = a;
    }

    
}
